extends Control

"""
卡牌点击诊断脚本 - 全面检测卡牌点击问题
"""

func _ready():
	print("=== 卡牌点击诊断开始 ===")
	
	# 等待一帧确保所有节点都已初始化
	await get_tree().process_frame
	
	# 查找所有卡牌节点
	var cards = find_all_cards()
	print("找到 ", cards.size(), " 张卡牌")
	
	for i in range(cards.size()):
		var card = cards[i]
		print("\n--- 诊断卡牌 ", i + 1, " ---")
		diagnose_card(card)
	
	print("\n=== 卡牌点击诊断完成 ===")

func find_all_cards() -> Array:
	"""递归查找所有卡牌节点"""
	var cards = []
	_find_cards_recursive(get_tree().root, cards)
	return cards

func _find_cards_recursive(node: Node, cards: Array):
	"""递归查找卡牌节点"""
	if node.has_method("set_card_data") or node.name.begins_with("Card"):
		cards.append(node)
	
	for child in node.get_children():
		_find_cards_recursive(child, cards)

func diagnose_card(card: Node):
	"""诊断单张卡牌的点击功能"""
	print("卡牌节点: ", card.name, " (", card.get_class(), ")")
	print("卡牌路径: ", card.get_path())
	print("卡牌位置: ", card.global_position if card.has_method("get_global_position") else "N/A")
	print("卡牌大小: ", card.size if card.has_method("get_size") else "N/A")
	
	# 检查节点类型
	if card is Control:
		print("✅ 卡牌是Control节点")
		
		# 检查mouse_filter设置
		print("mouse_filter: ", card.mouse_filter, " (", get_mouse_filter_name(card.mouse_filter), ")")
		
		# 检查focus_mode设置
		print("focus_mode: ", card.focus_mode, " (", get_focus_mode_name(card.focus_mode), ")")
		
		# 检查可见性
		print("visible: ", card.visible)
		print("modulate: ", card.modulate)
		
		# 检查层级
		print("z_index: ", card.z_index)
		
		# 检查信号连接
		check_signal_connections(card)
		
		# 检查脚本
		if card.get_script():
			print("✅ 卡牌有脚本: ", card.get_script().resource_path)
		else:
			print("❌ 卡牌没有脚本")
		
		# 检查父节点
		print("父节点: ", card.get_parent().name if card.get_parent() else "无")
		
		# 检查是否被其他节点遮挡
		check_occlusion(card)
		
	else:
		print("❌ 卡牌不是Control节点，类型: ", card.get_class())

func check_signal_connections(card: Control):
	"""检查信号连接情况"""
	print("--- 信号连接检查 ---")
	
	# 检查gui_input信号
	var gui_input_connections = card.gui_input.get_connections()
	print("gui_input信号连接数: ", gui_input_connections.size())
	for connection in gui_input_connections:
		print("  -> 连接到: ", connection.callable.get_object().name, ".", connection.callable.get_method())
	
	# 检查其他常用信号
	var signals_to_check = ["mouse_entered", "mouse_exited", "focus_entered", "focus_exited"]
	for signal_name in signals_to_check:
		if card.has_signal(signal_name):
			var connections = card.get_signal_connection_list(signal_name)
			if connections.size() > 0:
				print(signal_name, " 信号连接数: ", connections.size())

func check_occlusion(card: Control):
	"""检查卡牌是否被其他节点遮挡"""
	print("--- 遮挡检查 ---")
	
	var card_rect = Rect2(card.global_position, card.size)
	var overlapping_nodes = []
	
	# 检查同级节点
	var parent = card.get_parent()
	if parent:
		for sibling in parent.get_children():
			if sibling != card and sibling is Control:
				var sibling_control = sibling as Control
				var sibling_rect = Rect2(sibling_control.global_position, sibling_control.size)
				if card_rect.intersects(sibling_rect) and sibling_control.z_index >= card.z_index:
					overlapping_nodes.append(sibling)
	
	if overlapping_nodes.size() > 0:
		print("⚠️ 发现 ", overlapping_nodes.size(), " 个可能遮挡的节点:")
		for node in overlapping_nodes:
			print("  - ", node.name, " (z_index: ", node.z_index, ")")
	else:
		print("✅ 没有发现遮挡节点")

func get_mouse_filter_name(filter: int) -> String:
	"""获取mouse_filter的名称"""
	match filter:
		Control.MOUSE_FILTER_STOP:
			return "STOP"
		Control.MOUSE_FILTER_PASS:
			return "PASS"
		Control.MOUSE_FILTER_IGNORE:
			return "IGNORE"
		_:
			return "UNKNOWN"

func get_focus_mode_name(mode: int) -> String:
	"""获取focus_mode的名称"""
	match mode:
		Control.FOCUS_NONE:
			return "NONE"
		Control.FOCUS_CLICK:
			return "CLICK"
		Control.FOCUS_ALL:
			return "ALL"
		_:
			return "UNKNOWN"
